package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.windoent.yugioh.R;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.captcha.Captcha;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 * https://github.com/luozhanming/Captcha
 */
public class PictureVerifyDialog extends Dialog {
    private Captcha mCaptCha;

    private onClickListener listener;


    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public PictureVerifyDialog(Context context) {
        super(context, R.style.MoHUDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_verify_picture_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width =  ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {

    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        mCaptCha.setMaxFailedCount(10);
        mCaptCha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                dismiss();
                if (listener != null) {
                    listener.onClick();
                }
                return getContext().getString(R.string.string_verify_picture_sucess_cost_time) + time + getContext().getString(R.string.string_verify_picture_success_millions);
            }

            @Override
            public String onFailed(int failedCount) {
                ToastUtils.showText(getContext().getString(R.string.string_verify_picture_fail));
                return getContext().getString(R.string.string_verify_picture_has_failed) + failedCount + getContext().getString(R.string.string_verify_picture_fail_times);
            }

            @Override
            public String onMaxFailed() {
                ToastUtils.showText(getContext().getString(R.string.string_verify_picture_fail_out_times));
                return getContext().getString(R.string.string_verify_picture_fail_reason_out_times);
            }
        });
        mCaptCha.setDismiss(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mCaptCha = (Captcha) findViewById(R.id.captCha);
    }


    public interface onClickListener {
        public void onClick();
    }
}
